#include<Ticker.h>
#include<ESP8266WiFi.h>
#include <SoftwareSerial.h>

#define AP_SSID "CMCC-221"
#define AP_PSW "liliulin"
SoftwareSerial serial1(12, 14); 
const uint16_t port=8266;
const char* host="192.168.1.3";
WiFiClient client;
IPAddress local_IP(192,168,1,13);
IPAddress gateway(192,168,1,13);
IPAddress subnet(255,255,255,0);

Ticker timer;
// Assign output variables to GPIO pins
const int output5 = 5;
const int output4 = 4;
int Pulse_number = 0;   //脉冲数
int rotate = 0;   //转速
int rotate_set = 0;  //设定转速
int pwm = 0;

void Rotate_timer(){
//  rotate = 35*Pulse_number*60*100/370;
  rotate=Pulse_number*10*60/426;
  Pulse_number = 0;
//  Serial.print("当前转速为：");
//  Serial.println(rotate);
//  Serial.print("当前pwm为：");
//  Serial.println(pwm);
//  Serial.print("设定转速为：");
//  Serial.println(rotate_set);
}

void Rotate_timer1(){
  String comdata;
  while (Serial.available() > 0) // 串口收到字符数大于零。
  {   
    comdata += Serial.read();
  }
  if(comdata!="")//如果接受到数据
  {
    rotate = comdata.toInt();
    Serial.print("当前转速为：");
    Serial.println(rotate);
   }
}

void setup() {
  serial1.begin(9600);
  Serial.begin(115200);
  // Initialize the output variables as outputs
  pinMode(output5, OUTPUT);
  pinMode(output4, OUTPUT);
  pinMode(0,OUTPUT);
  
  digitalWrite(output5, LOW);
  digitalWrite(output4, HIGH);
  pinMode(10, INPUT_PULLUP);
  digitalWrite(0,1023);
  
  attachInterrupt(digitalPinToInterrupt(10), InterruptFunc, RISING);
  timer.attach(0.1,Rotate_timer);
  
  delay(1000);
  WiFi.mode(WIFI_AP_STA); 
  WiFi.begin(AP_SSID,AP_PSW);
  Serial.print("Wait for WiFi... ");
  //等待wifi连接成功
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  delay(500);
  
}

void loop(){
  //read_rotate();
  digitalWrite(0, pwm);
  wifiinit();
  PID_Concrol();
}

ICACHE_RAM_ATTR void InterruptFunc(){   //中断响应函数
  Pulse_number ++;
  //Serial.print("当前脉冲数为：");
  //Serial.println(Pulse_number);
}



 
void wifiinit() {
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    Serial.println("wait 5 sec...");
    delay(100);
  }else{
  // 发送数据到Tcp server
  Serial.println("Send this data to server");
  //client.println(String("Send this data to server"));
  client.println(rotate);
  //读取从server返回到响应数据
  String line = client.readStringUntil('\r');
  //Serial.println(line); 
  rotate_set = line.toInt();
  Serial.print("设定转速为：");
  Serial.println(rotate_set);
  Serial.print("此时pwm输出为：");
  Serial.println(pwm);
  //Serial.println("closing connection");
  //client.stop();
  //Serial.println("wait 5 sec...");
  //delay(5000);
  }
}

float kp = 0.1;
float ki = 0;
float kd = 0;
int error = 0;
int error_last = 0;
int error_last2 = 0;
int umax = 129;
int umin = -129;
int integral = 0;
void PID_Concrol(){
  error = rotate_set - rotate;
//  integral = integral + error;
//  if(integral > umax){
//    integral = umax;
//  }else if(integral < umin){
//    integral = umin;
//  }else{
//    pwm = kp * error + ki * integral+kd * (error-error_last);
//    error_last = error;
//  }
  pwm = pwm+kp*(error-error_last)+ki*error+kd*(error-2*error_last+error_last2);
  error_last2 = error_last;
  error_last = error;
  if(pwm>1023){
    pwm = 1023;
  }
  if(pwm<0){
    pwm = 0;
  }
}

void read_rotate(){
  if (serial1.available()){
  //size_t len =serial1.available();
  //uint8_t sbuf[len];
  //size_t j=0;
  String sbuf = "";
  while (serial1.available()) {
          //sbuf[j]=serial1.read();
          //j++;
          sbuf = sbuf + serial1.read();
        }
  //Serial.write(sbuf,len);
  rotate = sbuf.toInt();
  }
  
  delay(100);
}



 
